#!/usr/bin/perl
#vol_create.cgi

require './diskman-lib.pl';

&ReadParse();

my ($err_msg, $log_ev, $log_msg, $log_pri);

$err_msg="";
$log_ev="";

my $mkfs_lock;
$mkfs_lock = check_lock();
if ($mkfs_lock) {
	$err_msg = $text{"err_mkfslocked"};
	goto ERROR_OUT;
}

my (@rdinfo, @dinfo, @free_volumes, @free_disks, $i);

@rdinfo =  get_mega_raid_info();
@dinfo = get_all_lsi_diskinfo();

@free_volumes = &get_free_volumes(\@rdinfo);
@free_disks = &get_free_lsi_disks();

if (!@free_volumes) {
	$err_msg = $text{"err_configchanged"};
#	goto ERROR_OUT;
}

if (!@free_disks) {
	$err_msg = $text{"err_configchanged"};
#	goto ERROR_OUT;
}

#If has VG, then tell user reboot is needed when create a new raid;
#If no VG, then need not reboot.
&foreign_require("lvm", "lvm-lib.pl");
my @curvgs = &foreign_call("lvm", "get_vg_info");
my $alartreboot = 0;
if(@curvgs){
	$alartreboot = 1;
}
else{
	$alartreboot = 0;
}

&header($text{'vol_create_title'}, "", "vol_create", 0, 1);
print "<form name=createform action=save_create.cgi method=post onsubmit='return onsubmit_form($alartreboot);'>\n";

print "<table class=group width=100%>\n";
print "<tr> <td class=group><table class=mode1 border=0 cellpadding=3 cellspacing=0>\n";

#free volumes
#free volumes
print "<tr> <td width=250> <b> $text{'crt_volname'} </b> </td> \n";

print "<td width=200> <select name=volname class=drop> \n";
my ($fv);
$i = 0;
foreach $fv (@free_volumes) {
        if ($i == 0) {
                print "<option selected value=$fv> $fv \n";
        }
        else {
                print "<option value=$fv> $fv\n";
        }
        $i++;
}

print "</select>\n";
print "</td>\n";
print "</tr>\n";


#level
print "<tr> <td width=250> <b> $text{'crt_level'} </b> </td> \n";
print "<td width=200> <select name=level class=drop onchange='LevelSelected()'> \n";
print "<option selected value=0> $text{'crt_raid0'} \n";
#print "<option value=0> $text{'crt_raid0'}\n";
print "<option value=1> $text{'crt_raid1'}\n";
print "<option value=5> $text{'crt_raid5'}\n";
print "<option value=6> $text{'crt_raid6'}\n";
#print "<option value=9> SPARE\n";
print "</select>\n";
print "</td>\n";
print "</tr>\n";

print "</table> </td> </tr>\n";

#disk select section
print "<tr> <td class=group><table class=mode1 border=0 cellpadding=0 cellspacing=0>\n";
print "<tr>";

#free disk list
print "<td> <table class=mode1 border=0 cellpadding=3 cellspacing=0>\n";
print "<tr> <td width=250> <b> $text{'crt_freedisk'} </b></td> </tr> \n";
print "<tr> <td width=250> <select name=freedisk size=7 class=list>\n";
my ($fd);
$extmark = 1;
foreach $fd (@free_disks) {
	if($fd == 0){
		$extmark = 0;
	}
	$fd_n = $fd + 1 - $extmark;

	print "<option value=D$fd_n> D$fd_n\n";
}
print "</select>\n";
print "</td> </tr>\n";

print "</table> </td>\n";

#add and remove button
print "<td width=200> <table class=mode1 cellpadding=3 cellspacing=0>\n";
print "<tr> <td> <input type=button name=adddisk value=\"$text{'add'}\"  onclick='AddDisk();' class=button1></td> </tr>\n";
print "<tr> <td> <input type=button name=removedisk value=\"$text{'remove'}\" onclick='RemoveDisk();' class=button1></td> </tr>\n";
print "</table> </td>\n";

#selected disk list
print "<td width=250> <table class=mode1 cellpadding=3 cellspacing=0>\n";
print "<tr> <td> <b> $text{'crt_selecteddisk'} </b> </td> </tr>\n";

print "<tr> <td> <select name=selecteddisk size=7 class=list>\n";

#need script add or remove
print "</select>\n";
print "</td> </tr>\n";
print "</table> </td> </tr>\n";

print "</table> </td> </tr>\n";

#spare disk option
print "<tr> <td class=group><table class=mode1 border=0 cellpadding=3 cellspacing=0>\n";
print "<tr> <td width=250> <input type=checkbox name=needspare value='needspare' disabled=true onclick='NeedSpare(this)'>$text{'crt_needspare'} </td> \n";
print "<td width=200> <select name=sparedisk class=drop disabled=true visibled=true> </select> </td>\n";
print "</tr>\n";
print "</table> </td> </tr> \n";

#print create button
print "<tr> <td class=group><table class=mode1 border=0 cellpadding=3 cellspacing=0>\n";
print "<tr> <td> <input type=submit name=createvolume value=\"$text{'crt_create'}\"  onclick='return CreateVolume($alartreboot)' class=button1></td> </tr>\n";
print "</table> </td> </tr>\n";

print "</table> \n";

print "<input type=hidden name=listinput>\n";

#java script
print <<EOF;
<script>
function onsubmit_form(reboot_alart)
{
	//if (reboot_alart == 1) {
	//	alert("System will reboot when raid have been created. ");
	//}
        document.forms[0].createvolume.disabled = true;
        document.forms[0].adddisk.disabled = true;
        document.forms[0].removedisk.disabled = true;
}

function FindItemInSelectedList(value1)
{
	var rt = 0;
	var ii;

	for (ii = 0; ii < document.forms[0].selecteddisk.options.length; ii++) {
		if (document.forms[0].selecteddisk.options[ii].value == value1) {
			rt = 1;
		}
	}

	return rt;
}

function FindItemInSpareList(value1)
{
	var rt = 0;
	var ii;

	for (ii = 0; ii < document.forms[0].sparedisk.options.length; ii++) {
		if (document.forms[0].sparedisk.options[ii].value == value1) {
			rt = 1;
		}
	}

	return rt;
}

function LevelSelected()
{
        for (i = document.forms[0].level.options.length; i > 0; i--) {
		if (document.forms[0].level.options[i - 1].selected) {
			if ( (document.forms[0].level.options[i - 1].value == '1') ||
			(document.forms[0].level.options[i - 1].value == '5')) {
				document.forms[0].needspare.disabled=true;
				if (document.forms[0].needspare.checked) {
					document.forms[0].sparedisk.disabled=true;
				}
				else {
					document.forms[0].sparedisk.disabled=true;
				}
				
			}
			else {
				document.forms[0].needspare.disabled=true;
				document.forms[0].sparedisk.disabled=true;
			}
			break;
		}
	}
}

function AddDisk()
{
	var j = document.forms[0].selecteddisk.options.length;
	var k = document.forms[0].sparedisk.options.length;

	for (i = document.forms[0].freedisk.options.length; i > 0; i--) {
		if (document.forms[0].freedisk.options[i -1 ].selected) {
			if (FindItemInSelectedList(document.forms[0].freedisk[i-1].value) == 0) {
				document.forms[0].selecteddisk.options[j] =
					new Option(document.forms[0].freedisk.options[i-1].value, document.forms[0].freedisk.options[i-1].value);
				j = document.forms[0].selecteddisk.options.length;
			}
			if (FindItemInSpareList(document.forms[0].freedisk[i-1].value) == 0) {
				document.forms[0].sparedisk.options[k] = 
					new Option(document.forms[0].freedisk.options[i-1].value, document.forms[0].freedisk.options[i-1].value);
				k = document.forms[0].sparedisk.options.length;
			}
		}
	}


	if (document.forms[0].selecteddisk.options.length > 0) {
		document.forms[0].listinput.value = document.forms[0].selecteddisk.options[0].value;
	}
	else {
		document.forms[0].listinput.value = "";
	}

	for (i=1; i < document.forms[0].selecteddisk.options.length; i++) {
		document.forms[0].listinput.value += " " + document.forms[0].selecteddisk.options[i].value;
	}
}

function RemoveDisk()
{
	for (i = document.forms[0].selecteddisk.options.length; i > 0; i--) {
		if (document.forms[0].selecteddisk.options[i-1].selected) {
			for (j = document.forms[0].sparedisk.options.length; j > 0; j--) {
				if (document.forms[0].selecteddisk.options[i-1].value == document.forms[0].sparedisk.options[j-1].value) {
					document.forms[0].sparedisk.options[j - 1] = null;
				}
			}
			document.forms[0].selecteddisk.options[i-1] = null;
			if (document.forms[0].sparedisk.length > 0) {
				document.forms[0].sparedisk.options[0].selected = true;
			}
		}
	}

	if (document.forms[0].selecteddisk.options.length > 0) {
		document.forms[0].listinput.value = document.forms[0].selecteddisk.options[0].value;
	}
	else {
		document.forms[0].listinput.value = "";
	}

	for (i=1; i < document.forms[0].selecteddisk.options.length; i++) {
		document.forms[0].listinput.value += " " + document.forms[0].selecteddisk.options[i].value;
	}
}

function CreateVolume(alert_reboot)
{
	if (alert_reboot == 1) {
                        if (!confirm("$text{'create_reboot'}"))
                                return false;
	}
	var i;

	/* if (document.forms[0].level.options[0].selected) {
		//'Single' Only one disk allowed!
		if (document.forms[0].selecteddisk.options.length <= 0) {
			alert("$text{'crt_linearalert'}");
			return false;
		}
                if (document.forms[0].selecteddisk.options.length > 1) {
                        alert("$text{'crt_singlealert'}");
                        return false;
                }
		return true;
	} */
	if (document.forms[0].level.options[0].selected) {
		//'raid0'
		if (document.forms[0].selecteddisk.options.length <= 0) {
			alert("$text{'crt_raid0alert'}");
			return false;
		}
		return true;
	}
	else if (document.forms[0].level.options[1].selected) {
		//'raid1'
		if (document.forms[0].needspare.checked) {
			if (document.forms[0].selecteddisk.options.length <= 2) {
				alert("$text{'crt_raid1alert_spare'}");
				return false;
			}
		}
		else {
			if (document.forms[0].selecteddisk.options.length <= 1) {
				alert("$text{'crt_raid1alert'}");
				return false;
			}
		}
		return true;
	}
	else if (document.forms[0].level.options[2].selected) {
		//'raid5'
		if (document.forms[0].needspare.checked) {
			if (document.forms[0].selecteddisk.options.length <= 3) {
				alert("$text{'crt_raid5alert_spare'}");
				return false;
			}
		}
		else {
			if (document.forms[0].selecteddisk.options.length <= 2) {
				alert("$text{'crt_raid5alert'}");
				return false;
			}
		}
		return true;
	}
        else if (document.forms[0].level.options[3].selected) {
                //'raid6'
                if (document.forms[0].needspare.checked) {
                        if (document.forms[0].selecteddisk.options.length <= 4) {
                                alert("$text{'crt_raid6alert_spare'}");
                                return false;
                        }
                }
                else {
                        if (document.forms[0].selecteddisk.options.length <= 3) {
                                alert("$text{'crt_raid6alert'}");
                                return false;
                        }
                }
                return true;
        }
	else {
		return false;
	}
}

function NeedSpare(obj)
{
	if (obj.checked) {
		document.forms[0].sparedisk.disabled = false;
	}
	else {
		document.forms[0].sparedisk.disabled = true;
	}
}
</script>
EOF

#disk select section

print "</form>\n";

&footer("/megadm/", $text{'index'});

return;

ERROR_OUT:
&error_setup($text{'crt_err_title'});
&error($err_msg);
